//
//  ViewController.m
//  JGGaoDeDemo
//
//  Created by mac on 15/8/21.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<MAMapViewDelegate, CLLocationManagerDelegate>
{
    MAMapView *_mapView;
    
    CLGeocoder *_geocoder;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _geocoder=[[CLGeocoder alloc]init];
    
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;    //YES 为打开定位，NO为关闭定位
    
    //显示模式
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    [_mapView setZoomLevel:16.1 animated:YES];
    
    
    //后台定位
    _mapView.pausesLocationUpdatesAutomatically = NO;
    
    [self.view addSubview:_mapView];
    
}


-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
        // NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        //NSLog(@"%@",userLocation.title);
        // NSLog(@"%@",userLocation.subtitle);
        
        [self getAddressByLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
    }
}

#pragma mark 根据坐标取得地名
-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
    //反地理编码
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark=[placemarks firstObject];
        // NSLog(@"详细信息:%@",placemark.addressDictionary);
        
        NSLog(@"%@",[placemark.addressDictionary[@"FormattedAddressLines"] lastObject]);
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
